// pages/mine/address/index.js
const service = require('../../../service/port.js')
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    radio: {},
    addressList: [],
    router: '',
    showcheckbtn:false
  },
  /*单选*/
  onChange(event) {
    console.log(event.detail);

    this.setData({
      radio: event.detail,
    });
    if (wx.getStorageSync('isconfirmorder')) {
      var address = {};
      this.data.addressList.forEach(item => {
        if (item.id == event.detail) {
          address = item

        }
      })
      wx.setStorageSync('currentAddress', address)
      // wx.redirectTo({
      //   url: '/pages/order/confirmorder/index?address=' + JSON.stringify(address)
      // })
      wx.navigateBack({
        delta: 1
      })
    }

  },
  goAdd: function() {
    
    wx.navigateTo({
      url: '/pages/mine/addAddress/index',
    })
  },
  goDetail: function(e) {
    console.log(e);
    var address = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '/pages/mine/addressDetail/index?detail=' + JSON.stringify(address),
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options);
    if (options.router) {
      this.setData({
        router: options.router
      })
    }
    console.log(wx.getStorageSync('userInfo'));
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    service.getAddressList(wx.getStorageSync('userInfo').id, res => {
      console.log(res)
      this.setData({
        addressList: res.data
      })
    })
    if (wx.getStorageSync('isconfirmorder')) {
      this.setData({
        showcheckbtn:true
      })
    }else{
      this.setData({
        showcheckbtn:false
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})